package com.gxa.ehome.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//要实现一个切面编程,需要两个注解配合:
/*@Component
@Aspect*/
public class TxAspect {

//    @Resource(name="sqlSession")
    SqlSessionTemplate sqlSession;

    @Pointcut("execution(* com.gxa.ehome.service.*.*(..))")
    public void pointCut(){}

    //前置通知
    @Before("pointCut()")
    public void before(JoinPoint joinPoint){

    }

    //后置返回通知
    @AfterReturning("pointCut()")
    public void afterReturning(){
        sqlSession.close();
    }

    //后置抛出通知
    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        sqlSession.rollback();
    }

    //后置通知
    @After("pointCut()")
    public void after(){
        sqlSession.commit();
    }

}
